package io.lckgroup.modules.lck.utils;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.StartActivityTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.google.gson.JsonObject;

/**
 * 消息推送工具类
 *
 * @author srq
 * @email siruiqi@siruiqi.com
 * @date 2020-4-23 14:45:04
 */
public class UniPushUtils {
    // STEP1：获取应用基本信息
    private static String appId = "QRIGXFereo9NYhW3sbB013";
    private static String appKey = "OMxymqivT28ISJ5EJzKhx8";
    private static String masterSecret = "fgbLlFtefN7CW5ERm2R951";
    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";

    public static void main(String[] args) throws Exception {
        //推送订单信息
        //pushOrderMsg("9753ab02ba3d03ec4503756f635094cf","myorderData--value");
        //推送其它通知消息
        pushDealFeedBack("9753ab02ba3d03ec4503756f635094cf");
    }


    /**
     * 通过客户端id clientId，推送打印消息到手机端
     * @param cid of_shop_merchant_info表中的 client_id 值
     * @param orderData 要打印的字符串
     * @return 返回推送对象
     */
    public static IPushResult pushOrderMsg(String cid,String orderData) {
        IGtPush push = new IGtPush(url, appKey, masterSecret);
        // STEP4：选择通知模板
        TransmissionTemplate template = getTemplate(orderData);
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        // IOS通道下发策略
        message.setStrategyJson("{\"ios\":4}");
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        //target.setAlias(Alias);

        IPushResult ret = null;
        ret = push.pushMessageToSingle(message, target);
        System.out.println(ret.getResponse().toString());
        return ret;
    }



    /**
     * 获取模板信息
     * @param orderData 要透传打印的字符串
     * @return TransmissionTemplate
     */
    private static TransmissionTemplate getTemplate(String orderData) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        //android消息透传参数
        JsonObject jo = new JsonObject();
        jo.addProperty("payload",orderData);
        template.setTransmissionType(2);
        template.setTransmissionContent(jo.toString());
        template.setAPNInfo(getAPNPayload(orderData)); //ios消息推送
        //template.setAPNInfo(getVoIPPayload());
        return template;
    }

    /**
     * 获取透传消息
     * @param orderData 要透传打印的字符串
     * @return
     */
    private static APNPayload getAPNPayload(String orderData) {
        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
        payload.setAutoBadge("1");
        payload.setContentAvailable(0);
        //ios 12.0 以上可以使用 Dictionary 类型的 sound
        payload.setSound("default");
        //payload.setCategory("$由客户端定义");
        //iOS透传参数
        payload.addCustomMsg("payload", orderData);
        //简单模式APNPayload.SimpleMsg
        //payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));
        payload.setAlertMsg(getDictionaryAlertMsg());
        return payload;
    }


    /**
     * 设置通知栏的提示消息内容
     * @return
     */
    public static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg() {
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        alertMsg.setBody("亲!新到订单请及时处理");
        // iOS8.2以上版本支持
        alertMsg.setTitle("新到订单");
        return alertMsg;
    }


    /**
     * 处理信息反馈时，通知给商户的信息
     * @param cid 传入商户的客户端id，即of_shop_merchant_info表中的 client_id 值
     * @return IPushResult
     */
    public static IPushResult pushDealFeedBack(String cid) {
        IGtPush push = new IGtPush(url, appKey, masterSecret);
        // STEP1：选择模板
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);
        // STEP2：设置通知样式
        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle("反馈处理");
        style.setText("您的建议已处理");
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        style.setChannel("通知渠道id");
        style.setChannelName("通知渠道名称");
        style.setChannelLevel(3); //设置通知渠道重要性
        template.setStyle(style);

        template.setAPNInfo(getAPNPayload("您的建议已处理","反馈处理")); //详见本页iOS通知样式设置

        // STEP3：设置推送其他参数
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);

        // 厂商通道下发策略（可选项），参数含义见【推送API】页面
        message.setStrategyJson("{\"ios\":4,\"st\":4}");
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        //target.setAlias(Alias);

        IPushResult ret = null;
        ret = push.pushMessageToSingle(message, target);
        System.out.println(ret.getResponse().toString());
        return ret;
    }

    /**
     * ios端通知消息显示的样式
     * @param body
     * @param title
     * @return
     */
    private static APNPayload getAPNPayload(String body,String title) {
        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
        payload.setAutoBadge("1");
        payload.setContentAvailable(0);
        //ios 12.0 以上可以使用 Dictionary 类型的 sound
        payload.setSound("default");
        //payload.setCategory("$由客户端定义");
        //iOS透传参数
        //payload.addCustomMsg("payload", orderData);
        //简单模式APNPayload.SimpleMsg
        //payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));
        payload.setAlertMsg(getDictionaryAlertMsg(body,title));
        return payload;
    }

    /**
     * ios端通知消息显示的内容
     * @param body
     * @param title
     * @return
     */
    public static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String body,String title) {
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        alertMsg.setBody(body);
        // iOS8.2以上版本支持
        alertMsg.setTitle(title);
        return alertMsg;
    }

}
